//
//  ViewController.m
//  GridViewDemo
//
//  Created by James on 21/10/2017.
//  Copyright © 2017 James. All rights reserved.
//

#import "DQFormViewController.h"
#import "UIView+Layout.h"
#import "UtilsMacro.h"
#import "JHGridView.h"
#import "WModel.h"
#import "Model_Hr_Qc_Spec_Item.h"
#import "Model_Hr_InspectNew.h"
#import "Model_Hr_Qc_Spec.h"
#import "InitDbTable.h"
#import "HCommon.h"
@interface DQFormViewController ()<JHGridViewDelegate>
@property (nonatomic, strong)JHGridView *gridView;
@property (nonatomic, strong)NSArray *modelArray;
@property (nonatomic, copy) NSString *result_id;

@property (nonatomic, assign)BOOL isSaved;
@property (nonatomic, assign)BOOL isHasInspect;
@end



@implementation DQFormViewController


- (instancetype)initWithArray:(NSArray *)array
                    itemArray:(NSArray *)itemArray
                    result_id:(NSString *)result_id
                      isSaved:(BOOL)isSaved
{
    if (self) {
        self.array = array;
        self.itemArray = itemArray;
        self.result_id = result_id;
        self.isSaved = isSaved;
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    if (self.type != 2) {
        
        UIBarButtonItem *execbtn = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(didSaveButttonClicked:)];
        self.navigationItem.rightBarButtonItem = execbtn;
  
    }

    
    NSMutableArray *BigArray= [ NSMutableArray new];

    NSMutableArray *newArray = [NSMutableArray arrayWithArray:self.array];
    
    NSMutableArray *newItemArray = [NSMutableArray arrayWithArray:self.itemArray];
    
    
    Model_Hr_Qc_Spec *newModel = [[Model_Hr_Qc_Spec alloc]init];
    newModel.CONTENT_NAME = @"";
    
    Model_Hr_Qc_Spec_Item *model = [[Model_Hr_Qc_Spec_Item alloc]init];
    model.ITEM_NAME = @"总检查数";
    model.ITEM_ID = @"ALL";
    
    [newItemArray insertObject:model atIndex:0];
    
    Model_Hr_Qc_Spec_Item *newItemModel = [[Model_Hr_Qc_Spec_Item alloc]init];
    newItemModel.ITEM_NAME = @"符合数";
    newItemModel.ITEM_ID = @"ALLFHS";
    [newItemArray insertObject:newItemModel atIndex:1];
    [newArray insertObject:newModel atIndex:0];
    
    NSMutableArray *itemArray = [NSMutableArray new];
    
    for (Model_Hr_Qc_Spec *modelItem in self.array) {
        
        [itemArray addObject:modelItem.CONTENT_NAME];
    }
    
    NSMutableArray *specArray = [NSMutableArray new];
    
    for (int j = 0; j < newArray.count - 1 ; j ++) {
        NSMutableArray *array = [NSMutableArray new];
        for (int i = 0; i < newItemArray.count; i ++) {
            
            Model_Hr_InspectNew *model = [[Model_Hr_InspectNew alloc]init];
            
            
            [array addObject:model];
        }

        [specArray addObject:array];
    }
    
    
    for (int i = 1; i < newArray.count ; i ++ ) {
        
        Model_Hr_Qc_Spec *model = newArray[i];
        
        NSMutableArray *aray = [NSMutableArray new];
        
        for (int j = 0; j < self.editArray.count; j ++) {
            
            Model_Hr_InspectNew *model1 = self.editArray [j ];
            
            if ([model1.CONTENT_ID isEqualToString:model.CONTENT_ID]) {
                
                [aray addObject:model1];
            }
        }
        
        [BigArray addObject:aray];
        
        if (aray.count == 0) {
            
            BigArray = specArray;
            
            self.isHasInspect = 1;
        }
        
    }
    

    
        if (self.isSaved == 0) {
           
            if (self.editArray.count > 0) {
                [specArray removeAllObjects];
                [specArray addObject:self.editArray];
            }
            
        [self.gridView configureViewWithTitlesArray:(NSArray *)newArray
                                           titleKey:@"CONTENT_NAME"
                                          leftArray:(NSArray *)newItemArray
                                            leftKey:@"ITEM_NAME"
                                         itemsArray:specArray
                                           itemKeys:@[@"ITEM_ID",@"CONTENT_ID",@"RESULT_ID",@"CODE_ID",@"QUANTITY",@"BUCKLESCORE"]
                                          restlt_id:self.result_id isSaved:self.isSaved isHasIpspect:self.isHasInspect code_id:self.code_id type:self.type];

    }else {
        [self.gridView configureViewWithTitlesArray:(NSArray *)newArray
                                           titleKey:@"CONTENT_NAME"
                                          leftArray:(NSArray *)newItemArray
                                            leftKey:@"ITEM_NAME"
                                         itemsArray:BigArray
                                           itemKeys:@[@"ITEM_ID",@"CONTENT_ID",@"RESULT_ID",@"CODE_ID",@"QUANTITY",@"BUCKLESCORE"]
                                          restlt_id:self.result_id isSaved:self.isSaved isHasIpspect:self.isHasInspect code_id:self.code_id type:self.type];

    }
    [self.view addSubview:self.gridView];
}

- (JHGridView *)gridView {
    if (!_gridView) {
        _gridView = [[JHGridView alloc]initWithFrame:CGRectMake(12, 64, ScreenWidth-24, 300)];
        _gridView.delegate = self;
    }
    return _gridView;
}
- (UIColor *)textColorForGridAtGridIndex:(GridIndex)gridIndex {
    return [UIColor blackColor];
}
- (CGFloat)widthForColAtIndex:(long)index {
    if (index == 0) {
        return 120;
    }
    return 90;
}

- (UIFont *)fontForTitleAtIndex:(long)index {
    return [UIFont systemFontOfSize:13];
}

- (UIFont *)fontForGridAtGridIndex:(GridIndex)gridIndex {
    return [UIFont systemFontOfSize:13];
}

- (JHGridAlignmentType)gridViewAlignmentTypeWithGridIndex:(GridIndex)gridIndex {
    if (gridIndex.col == 0) {
        return JHGridAlignmentTypeLeft;
    }
    return JHGridAlignmentTypeCenter;
}

- (BOOL)hiddenViewForGridAtGridIndex:(GridIndex)gridIndex {
    if (gridIndex.col == 0) {
        return YES;
    }
    return NO;
}

- (void)textFileText:(NSString *)text
             section:(long)section
                 row:(long)row
               array:(NSArray *)array
{
    self.modelArray = array;
    
    
}
- (void)didSaveButttonClicked:(UIButton *)sender
{
    
    [HCommon popupSuccessMessage:@"保存成功"];
    InitDbTable *init = [[InitDbTable alloc]init];
    
    if (self.isSaved == 0) {
        
        
        
        [init insetInspectNew:self.modelArray];

    }else {
        
        if (self.isHasInspect == 1) {
            
            if (self.editArray.count > 0) {
                
                [init updateInspectNew:self.modelArray];

            }else {
                
                [init insetInspectNew:self.modelArray];

            }
            

        }else {
            [init updateInspectNew:self.modelArray];

        }
    }
    
    NSArray *array = self.modelArray[0];
    
    Model_Hr_InspectNew *model = array[0];
    
    [self.navigationController popViewControllerAnimated:YES];
    __weak DQFormViewController *weakSelf = self;

    if (self.isSaved == 0) {
        
        weakSelf.getBlock (model.RESULT_ID);

    }else {
        weakSelf.getBlock (self.result_id);
    }
}



- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
 
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
